package ClientFront.Net;


import java.io.Serializable;
import java.util.Locale;

public enum MsgTypeEnum implements Serializable {

    GETUSERNUM,
    RETUSERNUM,USERQUIT,WIN,LOSE,USEWHITE,USEBLACK,GAMESTART,YOURTURN,RESTART,
    LINKERROR,LINKTEST,LINKACK,MSG,STEP,ACT,ACTCANCEL,ACTACK,ACTDENY,QUIT,CLIENT_TIP;
    private static final long serialVersionUID = 43L;

    private static final String[][] args = {
            {},{"uid"}, {"uidList","ipList"}, {"text"}, {"point"}, {"type"}, {"type"}, {}, {}, {}
    };
    public static int getValue(MsgTypeEnum type){
        int ret = -1;
        switch (type){
            case LINKERROR  ->  ret= 0;
            case LINKTEST   ->  ret= 1;
            case LINKACK    ->  ret= 2;
            case MSG        ->  ret= 3;
            case STEP       ->  ret= 4;
            case ACT        ->  ret= 5;
            case ACTCANCEL  ->  ret= 6;
            case ACTACK     ->  ret= 7;
            case ACTDENY    ->  ret= 8;
            case QUIT       ->  ret= 9;
        }
        return ret;
    }

    public static String getHeaderForm(MsgTypeEnum type){
        StringBuffer sb = new StringBuffer();
        sb.append("msg_");
        sb.append(type.name().toLowerCase(Locale.ROOT));
        sb.append("?");
        int tindex = getValue(type);
        for (int i = 0; i < args[tindex].length; i++) {
            sb.append(args[tindex][i]);
            sb.append("=");
            if(i < args[tindex].length-1){
                sb.append("&");
            }
        }
        return sb.toString();
    }

    public static String getHeader(MsgTypeEnum type){
        return ("msg_" + type.name()).toUpperCase(Locale.ROOT);
    }

    public static MsgTypeEnum getType(String header){
        header = header.substring(header.indexOf('_')+1).toUpperCase(Locale.ROOT);
        return MsgTypeEnum.valueOf(header);
    }

    public static int getArgsNum(MsgTypeEnum type){
        return args[getValue(type)].length;
    }
}
